﻿#include "cmdwidget.h"
#if _MSC_VER >=1600
#pragma execution_character_set("utf-8")
#endif
QCmdWidget::QCmdWidget(st_cmd cmd,QWidget *parent) : QWidget(parent)
{
    _my_cmd = cmd;
    _pCmdButtonGroup = new QButtonGroup();

    InitUI();

    void (QButtonGroup::*fn)(int) = &QButtonGroup::buttonClicked;
    connect(_pCmdButtonGroup,fn,this,&QCmdWidget::onCmdButtonClick);

    _pCmdButtonGroup->buttons()[0]->setChecked(true);
    onShowParamWidget(0x0);
}

/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
void QCmdWidget::InitUI()
{
    QLabel* pWidgetNamalabel= new QLabel(this);
    pWidgetNamalabel->setText(_my_cmd.sName);
    pWidgetNamalabel->setStyleSheet("font:24px 'Microsoft YaHei';font-weight:bold;color:white");
    pWidgetNamalabel->setMaximumHeight(40);

    _pNoneButtonLabel = new QLabel("无参数");
    _pNoneButtonLabel->setAlignment(Qt::AlignCenter);

    QWidget* pBtn1Widget = createButtonStatusWidget(_pCmdButtonGroup);
    createParamWidget();

    QGridLayout* playout = new QGridLayout(this);

    for(int i = 0; i < 6; i++)
    {
        playout->setColumnStretch(i,1);
    }

    int nRow = 0;


    playout->addWidget(pWidgetNamalabel,nRow,0,1,2);
    nRow++;
    nRow++;
    playout->addWidget(pBtn1Widget,nRow,0,2,5);

    nRow++;
    nRow++;

    for(QWidget* pItemWidget:m_cmd_paraWidget.values())
    {
        playout->addWidget(pItemWidget,nRow,0,5,5);
    }

    nRow = nRow + 5;

    for(int i = 0; i <= nRow; i++)
    {
        playout->setRowStretch(i,1);
    }

}


/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
QWidget* QCmdWidget::createButtonStatusWidget(QButtonGroup* pGroup)
{
    QList<QPushButton*> l_button1_list;

    for(int i = 0; i < _my_cmd.cmdlist.keys().size();i++)
    {
        int key = _my_cmd.cmdlist.keys()[i];
        st_cmd_info l_cmd_info = _my_cmd.cmdlist[key];

        QPushButton* pBtn = new QPushButton(l_cmd_info.sCmdName,this);
        pBtn->setCheckable(true);
        l_button1_list.append(pBtn);
        setButtonStyleSheet(pBtn);
        pGroup->addButton(pBtn,l_cmd_info.nCmdId);
    }

    QWidget* pwidget = new QWidget(this);
    QGridLayout* playout = new QGridLayout(pwidget);

    for(int i = 0; i < l_button1_list.size(); i++)
    {
        playout->addWidget(l_button1_list[i],i/4,i%4,1,1);
    }

    for(int i = 0; i < 4; i++)
    {
        playout->setColumnStretch(i,1);
        playout->setColumnMinimumWidth(i, 100);
    }


    return pwidget;
}

/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
void QCmdWidget::createParamWidget()
{
    //创建参数界面
    for(int i = 0; i < _my_cmd.cmdlist.keys().size();i++)
    {
        int nKey = _my_cmd.cmdlist.keys()[i];
        st_cmd_info l_cmd_info = _my_cmd.cmdlist[nKey];

        if(l_cmd_info.nAttrType == 0x00)//无参数
        {
            m_cmd_paraWidget.insert(l_cmd_info.nCmdId,_pNoneButtonLabel);
        }

        if(l_cmd_info.nAttrType == 0x01)//带命令参数
        {
            QMap<int,QString> l_cmd_attrMap;
            foreach (st_cmd_attr ItemAttr, l_cmd_info.cmdAttrlist)
            {
                l_cmd_attrMap[ItemAttr.nAttrValue]=ItemAttr.sAttrName;
            }
            QCmdParamWidget* pCmdParamWidget = new QCmdParamWidget(l_cmd_info.sCmdName,l_cmd_info.nCmdId,l_cmd_attrMap,this);
            m_cmd_paraWidget.insert(l_cmd_info.nCmdId,pCmdParamWidget);
            connect(pCmdParamWidget,&QCmdParamWidget::signal_set_cmd_value,this,&QCmdWidget::onSendData);
        }


        if(l_cmd_info.nAttrType == 0x02)//带值参数
        {
            QStringList l_cmd_namelist;
            foreach (st_cmd_attr ItemAttr, l_cmd_info.cmdAttrlist)
            {
                l_cmd_namelist.append(ItemAttr.sAttrName);
            }
            QDataParamWidget* pCmdParamWidget = new QDataParamWidget(l_cmd_info.sCmdName,l_cmd_info.nCmdId,l_cmd_namelist,this);
            m_cmd_paraWidget.insert(l_cmd_info.nCmdId,pCmdParamWidget);
            connect(pCmdParamWidget,&QDataParamWidget::signal_set_data_value,this,&QCmdWidget::onSendData);
        }
    }

}

/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
void QCmdWidget::onCmdButtonClick(int buttonId)
{
    //无参数 直接发送
     st_cmd_info l_cmd_info = _my_cmd.cmdlist[buttonId];
     if(l_cmd_info.nAttrType == 0x0)
     {
         QByteArray l_data;
         l_data.append((uchar)buttonId);
         emit signal_sendData(_my_cmd.nIndex,l_cmd_info.nCmdId,l_data,1);
     }

    onShowParamWidget(buttonId);

//    setButtonEnable(buttonId);
}

/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
void QCmdWidget::setButtonStyleSheet(QPushButton* pBtn)
{

    pBtn->setStyleSheet(
                "QPushButton:checked { background-color: rgb(245,90,70);;color:white;}"
                "QPushButton:unchecked { background-color: rgb(255,255,255);;color:black;}"
                "QPushButton{background-color: rgb(255,255,255);padding:5px;border-radius:5px;color:black;"
                "font-size:20px;font-family: 'Microsoft YaHei';}"
                "QPushButton:disabled { background-color: rgb(200,200,200);color:black;}"

                                );


//    pBtn->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    pBtn->setMinimumHeight(40);
}

/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
void QCmdWidget::setParamWidgetHide(bool bhide)
{
    for(QWidget* pwidget:m_cmd_paraWidget.values())
    {
        pwidget->setMinimumHeight(200);
        pwidget->setHidden(bhide);
    }
}


/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
void QCmdWidget:: onShowParamWidget(int nIndex)
{
    QWidget* pTempWidget = nullptr;

    setParamWidgetHide(true);

    pTempWidget = m_cmd_paraWidget.value(nIndex);

    if(pTempWidget != nullptr)
    {
        pTempWidget->setHidden(false);
    }

}

/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
void QCmdWidget::onSendData(int nCmdItemId,QByteArray data,int nItemByteNum)
{
    emit signal_sendData(_my_cmd.nIndex,nCmdItemId,data,nItemByteNum);
}

///**
//* @brief
//* @note
//*
//*/
//void QCmdWidget::setButtonEnable(int clickBtnId)
//{
//    if(_my_cmd.nIndex != 0x01)
//    {
//        return;
//    }

//    if(clickBtnId == 0x01 || clickBtnId == 0x09)//手动跟踪 AI跟踪
//    {
//        for(int i = 0; i< _my_cmd.cmdlist.keys().size();i++)
//        {
//            int key = _my_cmd.cmdlist.keys()[i];
//             st_cmd_info l_cmd_info = _my_cmd.cmdlist[key];
//            if(l_cmd_info.nCmdId != 0x05 && l_cmd_info.nCmdId < 0xf0)//解除跟踪
//            {
//                _pCmdButtonGroup->button(l_cmd_info.nCmdId)->setEnabled(false);
//            }
//        }
//    }

//    if(clickBtnId == 0x05)//解除跟踪
//    {
//        for(int i = 0; i< _my_cmd.cmdlist.keys().size();i++)
//        {
//            int key = _my_cmd.cmdlist.keys()[i];
//             st_cmd_info l_cmd_info = _my_cmd.cmdlist[key];
//             if(l_cmd_info.nCmdId < 0xf0)//解除跟踪
//             {
//                _pCmdButtonGroup->button(l_cmd_info.nCmdId)->setEnabled(true);
//             }
//        }
//    }

//}


/**
* @brief
* @note
*
*/
void QCmdWidget::setButtonEnable(int clickBtnId,bool bEnable)
{
    for(int i = 0; i< _my_cmd.cmdlist.keys().size();i++)
    {
        int key = _my_cmd.cmdlist.keys()[i];
         st_cmd_info l_cmd_info = _my_cmd.cmdlist[key];
        if(l_cmd_info.nCmdId == clickBtnId)
        {
            _pCmdButtonGroup->button(l_cmd_info.nCmdId)->setEnabled(bEnable);
            break;
        }
    }

}


/**
* @brief
* @note
*
*/
void QCmdWidget::setButtonChecked(int clickBtnId,bool bchecked)
{
    for(int i = 0; i< _my_cmd.cmdlist.keys().size();i++)
    {
        int key = _my_cmd.cmdlist.keys()[i];
         st_cmd_info l_cmd_info = _my_cmd.cmdlist[key];
        if(l_cmd_info.nCmdId == clickBtnId)
        {
            _pCmdButtonGroup->button(l_cmd_info.nCmdId)->setChecked(bchecked);
            break;
        }
    }
//    _pCmdButtonGroup->button(clickBtnId)->setChecked(bchecked);
}

/**
* @brief
* @note
*
*/
bool QCmdWidget::setParamData(int cmdId,QList<float> valueList)
{
    QDataParamWidget* pWidget = (QDataParamWidget*)m_cmd_paraWidget[cmdId];
    pWidget->setParamData(valueList);
    return true;
}
